<?php

#namespace humhub\modules\space\controllers;
namespace humhub\modules\api\controllers;

use Yii;
use humhub\modules\space\models\Space;
use humhub\modules\user\models\User;
use humhub\modules\user\widgets\UserListBox;
use humhub\modules\space\models\Setting;
use humhub\modules\content\models\Content;
use humhub\modules\post\models\Post;

use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;

/**
 * 编辑Space基本设置, 隐私
 */
class SpaceEditController extends Base2Controller
{

    /**
     * @inheritdoc
     */
    public function init(){
        $_GET['sguid'] = Yii::$app->request->get('s');
    }

    /**
     * @inheritdoc
     */
    public function behaviors(){
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                //QueryParamAuth::className(),
                HttpBearerAuth::className(),
            ],
        ];

        $behaviors['acl'] = [
            'class' => \humhub\components\behaviors\AccessControl::className(),
        ];

        return $behaviors;
    }

    /**
     * @inheritdoc
     */
    public function actions(){
        $actions = parent::actions();
        unset($actions['index'], $actions['view'], $actions['delete'], $actions['update'], $actions['create']);
        return $actions;
    }

    /**
     * 获取Space板块基本信息
     *
     * GET /api/space-edit?s=[guid]
     *
     * get.s 板块Space的guid
     *
     * @return array color: 板块颜色,格式:#d1d1d1,  description: 板块描述,  name: 板块名称,  tags: 标签,  visibility: 可见性: 1=Public, 0=Private,  join_policy: 加入政策: 1=邀请和请求, 2=Everyone can enter,  default_content_visibility: Default content visibility : 1=Public, 0=Private, 
     */
    public function actionIndex(){
        $this->adminOnly();
        $space = $this->contentContainer;
        return array(
            'color' => $space->color,
            'name' => $space->name,
            'description' => $space->description,
            'tags' => $space->tags,
            'visibility' => $space->visibility,
            'join_policy' => $space->join_policy,
            'default_content_visibility' => $space->default_content_visibility,
        );
    }

    /**
     * 编辑Space板块基本信息
     *
     * POST /api/space-edit/edit?s=[guid]
     *
     * get.s 板块Space的guid
     *
     * post.Space[color]  板块颜色,格式:#d1d1d1
     *
     * post.Space[description]  板块描述
     *
     * post.Space[name]   板块名称
     *
     * post.Space[tags]  标签
     *
     * post.Space[visibility]  可见性: 1=Public, 0=Private
     *
     * post.Space[join_policy]  加入政策: 1=邀请和请求, 2=Everyone can enter
     *
     * post.Space[default_content_visibility]  Default content visibility : 1=Public, 0=Private
     *
     * @return boolean 成功返回true; 如果失败data包含错误原因,需遍历data显示对应的错误
     */
    public function actionEdit(){
        $this->adminOnly();
        $space = $this->contentContainer;
        $space->scenario = 'edit';

        if ($space->load(Yii::$app->request->post()) && $space->validate() && $space->save()) {
            return true;
        }
        Yii::$app->params['res-success'] = false;
        return $space->getErrors();
    }

    /**
     * @inheritdoc
     */
    public function adminOnly(){
        parent::init();
        if (!$this->getSpace()->isAdmin())
            throw new \yii\web\HttpException(403, 'Access denied - Space Administrator only!');
    }
}
?>
